package skill;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import pluginMain.SP;
import skill.SkillBase.SkillType;

public class SkillManager {
	private List<SkillPlayer> playerSkills = new ArrayList<SkillPlayer>();
	private List<SkillBase> allSkills = new ArrayList<SkillBase>();
	private Plugin parentPlugin;

	public SkillManager(Plugin p) {
		parentPlugin = p;
	}

	public void AddNewPlayer(Player player) {
		for (SkillPlayer g : playerSkills) {
			if (g.getOwner().equals(player))
				return;
		}
		playerSkills.add(new SkillPlayer(allSkills, player));
	}

	public void AddNewSkill(int id, String name, SkillType t, double effect) {
		allSkills.add(new SkillBase(id, name, t, effect));
	}

	public SkillPlayer getPlayerSkills(Player player) {
		for (SkillPlayer g : playerSkills) {
			if (g.getOwner().equals(player))
				return g;
		}
		return null;
	}

	public void showLevels(Player user) {
		user.sendMessage(ChatColor.GOLD + "====当前技能等级====");
		for (Skill s : SP.sm.getPlayerSkills(user).getAllSkill()) {
			user.sendMessage("技能" + ChatColor.YELLOW + s.getSkillBase().getSkillName() + ChatColor.WHITE + " 等级 Lv."
					+ ChatColor.GREEN + s.getLevel() + ChatColor.WHITE + ", 升级经验:" + s.getNextLevelExp());
		}
		user.sendMessage("您可以输入learn 技能名称，进行技能学习！");
	}

	public void showSkills(Player user) {
		user.sendMessage(ChatColor.GOLD + "当前可加点技能");
		for (SkillBase s : allSkills) {
			Skill p = SP.sm.getPlayerSkills(user).getSkillByName(s.getSkillName());
			if (p != null) {
				user.sendMessage("技能" + ChatColor.YELLOW + s.getSkillName() + ChatColor.WHITE + ", 等级 Lv."
						+ ChatColor.GREEN + p.getLevel() + ChatColor.WHITE + " 升级经验:" + p.getNextLevelExp());
			} else {
				user.sendMessage("技能" + ChatColor.YELLOW + s.getSkillName());
			}
		}
	}

	public boolean showOtherPlayerSkill(CommandSender sender, String userName) {
		@SuppressWarnings("deprecation")
		Player user = SP.instance.getServer().getPlayer(userName);
		if (user == null || user.equals(null))
			return false;
		sender.sendMessage(ChatColor.GOLD + "====当前技能等级====");
		for (SkillBase s : allSkills) {
			Skill p = SP.sm.getPlayerSkills(user).getSkillByName(s.getSkillName());
			if (p != null) {
				sender.sendMessage("技能" + ChatColor.YELLOW + s.getSkillName() + ChatColor.WHITE + ", 等级 Lv."
						+ ChatColor.GREEN + p.getLevel() + ChatColor.WHITE + " 升级经验:" + p.getNextLevelExp());
			} else {
				sender.sendMessage("技能" + ChatColor.YELLOW + s.getSkillName());
			}
		}
		return true;
	}

	public SkillBase getSkillBaseByName(String name) {
		for (SkillBase b : allSkills) {
			if (b.getSkillName().equals(name))
				return b;
		}
		return null;
	}

	public int getSkillCount() {
		return allSkills.size();
	}

	public int getPlayerCount() {
		return playerSkills.size();
	}

	public Plugin getPlugin() {
		return parentPlugin;
	}
}
